package com.jic.market.controller.manage;

import com.jic.common.base.vo.PageResult;
import com.jic.common.base.vo.RestResult;
import com.jic.manager.api.rest.ManagerMerchantApi;
import com.jic.market.request.*;
import com.jic.market.request.manager.single.PromotionSingleCopyRequest;
import com.jic.market.response.PromotionProductResponse;
import com.jic.market.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


/**
 * date: 2020-03-12 18:20
 * author: zxl
 * version: 1.0
 */
@RestController
@RequestMapping("promotionSingle")
@Api(tags = {"单一促销活动"})
@Slf4j
public class PromotionSingleController{

    @Autowired
    PromotionMerchantProductService promotionMerchantProductService;

    @Autowired
    PromotionLimitRuleService promotionLimitRuleService;

    @Resource
    private PromotionSingleLimitRuleService promotionSingleLimitRuleService;

    @Resource
    ManagerMerchantApi managerMerchantApi;

    /*
     * 单一促销详情
     * */
    @ApiOperation(value = "查询单一促销详情")
    @RequestMapping("/getSingleProductList")
    @ResponseBody
    public RestResult<PageResult<PromotionProductResponse>> getPromotionProductList(@RequestBody PromotionProductQueryListRequest ppqlRequest) {
        PageResult<PromotionProductResponse> pageResult = promotionSingleLimitRuleService.getPromotionProductList(ppqlRequest);
        return RestResult.success(pageResult);
    }



    /*
    * 批量保存单一促销产品数据
    * */
    @ApiOperation(value = "批量保存单一促销产品数据")
    @PostMapping("updateSinglePriceOrLimit")
    @ResponseBody
    public RestResult updateSinglePriceOrLimit(@RequestBody PromotionLimitRuleRequest request) {
        String [] ids =request.getIds().split(",");
        for (int i= 0;i<ids.length;i++){
            request.setMpId(Long.parseLong(ids[i]));
            // 修改 促销商品表(商家) promotion_merchant_product
            promotionMerchantProductService.updateEntityByPromotionIdAndMpId(request.getPromPrice(),request.getPromotionId(),request.getMpId());
            // 修改 促销限制规则表 promotion_limit_rule
            promotionLimitRuleService.updatePriceOrLimit(request);
            // 修改 单一促销代理商规则表 promotion_single_rule
            promotionSingleLimitRuleService.updatePriceOrLimit(request);
        }
        return RestResult.success(null);
    }
    /*
     * 批量删除单一促销产品数据
     * */
    @ApiOperation(value = "批量删除单一促销产品数据")
    @PostMapping("deleteSingleProduct")
    @ResponseBody
    public RestResult deletePromotionMerchantProduct(@RequestBody DeletePromotionMerchantProductRequest request) {
        //  根据mpId ，promotionId 查询
        //  1. 如果是普通商品可以直接删除
        //  2. 如果是子品，需要先查询有没有其他同样父品的子品
        //  3. 如果还有其他子品，则只删除此子品
        //  4. 如果没有其他子品，则主品和此子品
        String [] mpids = request.getIds().split(",");
        for(int i = 0; i <mpids.length; i++ ){
            // 删除 promotion_merchant_product
            Integer count1 = promotionMerchantProductService.deleteEntityByPromotionIdAndMpId(
                    request.getPromotionId(),Long.parseLong(mpids[i]));
            // 删除 promotion_limit_rule
            Integer count2 = promotionLimitRuleService.deleteEntityByPromotionIdAndMpId(
                    request.getPromotionId(),Long.parseLong(mpids[i]));
            // 删除 promotion_single_rule
            Integer count3 = promotionSingleLimitRuleService.deleteEntityByPromotionIdAndMpId(
                    request.getPromotionId(),Long.parseLong(mpids[i]));
        }

        return RestResult.success(1);
    }

    /*
    * 复制单一活动
    * */
    @ApiOperation(value = "复制")
    @PostMapping("copyPromotionSingle")
    public RestResult<Long> copyPromotionSingle(@RequestBody PromotionSingleCopyRequest request) {
        return promotionSingleLimitRuleService.copy(request);
    }

    /*
     * 保存为模板
     * */
    @ApiOperation(value = "保存为模板")
    @PostMapping("saveTemplate")
    public RestResult<Long> saveTemplate(@RequestBody PromotionSingleCopyRequest request) {
        return promotionSingleLimitRuleService.saveTemplate(request);
    }

    /*
     * 批量删除单一活动
     * */
    @ApiOperation(value = "批量删除单一促销产品数据")
    @PostMapping("deletePromotionSingle")
    @ResponseBody
    public RestResult deletePromotionSingle(@RequestBody PromotionSingleCopyRequest request) {
            promotionSingleLimitRuleService.deletePromotionSingle(request.getPromotionId());
        return RestResult.success(1);
    }

    /**
     * 单一活动批量添加商品
     * @param request
     * @return
     */
    @PostMapping("saveBatchPromotionalProduct")
    @ApiOperation(value = "活动批量添加商品")
    public RestResult saveBatchPromotionalProduct(@RequestBody PromotionProductSaveBatchListRequest request) {
        return RestResult.success(promotionSingleLimitRuleService.saveSingleMerchantProduct(request));
    }

    /**
     * 插入促销商品限制
     * @param ruleRequest
     * @return
     */
    @ResponseBody
    @RequestMapping("insert")
    public RestResult<Long> insert(@RequestBody PromotionLimitRuleRequest ruleRequest) {
        return RestResult.success(promotionSingleLimitRuleService.insertLimit(ruleRequest));
    }

}
